from httprunner import HttpRunner,Step,RunRequest,RunTestCase,Config
from common.ConfigReader import ConfigReader
from business.test_login import TestCaseLogin as Login
base_path = ConfigReader('config.ini').get_Url('dev_base_url')
class API_START(HttpRunner):
    config = Config('开始').base_url(base_path).verify(False)
    teststeps = [
        Step(
            RunTestCase(
                "登录"
            ).call(Login).export(*['token',])
        )
    ]
def reset_token(login=Login):
    API_START.teststeps.clear() # 先清空所有的token
    API_START.teststeps.append(
        Step(
            RunTestCase(
                "登录"
            ).
                call(login).
                export(*['token', ])
        )
    )
headers = {'Authori-zation': 'Bearer ${token}'}
def send_api(method,requests_name,requests_path,requests_body=None,requests_headers=headers):
    if method =='post':
        return RunRequest(
            requests_name
        ).\
        post(requests_path).\
        with_headers(**requests_headers).\
        with_json(requests_body).\
        extract().\
        with_jmespath("body",'res_data')
    elif method =='get':
        return RunRequest(
            requests_name
        ). \
            get(requests_path). \
            with_headers(**requests_headers). \
            extract(). \
            with_jmespath("body", 'res_data')
    elif method =='delete':
        return RunRequest(
            requests_name
        ). \
            delete(requests_path). \
            with_headers(**requests_headers). \
            extract(). \
            with_jmespath("body", 'res_data')
def step_obj(obj):
    return Step(
        obj
    )
def get_last_response(obj):
    API_START.teststeps.extend(obj)  # 可以存放很多个步骤
    api = API_START()
    api.test_start() # 开始执行
    res = api.with_export(['token','res_data']).get_export_variables()
    return res  # {“res_data”:"xxxx"}
























